package org.zhxy.common.utils;

import com.github.tobato.fastdfs.domain.fdfs.StorePath;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

public class UploadUtils {

    protected  String uploadFile(FastFileStorageClient storageClient, File file) throws FileNotFoundException {
        // 上传并且生成缩略图
        StorePath storePath = storageClient.uploadFile(
                new FileInputStream(file),
                file.length(),
                file.getName().substring(file.getName().lastIndexOf(".") + 1),
                null
        );

        return storePath.getFullPath();
    }

    protected String uploadFile (FastFileStorageClient storageClient, MultipartFile file) throws IOException {

        String originalFilename = file.getOriginalFilename();
        String substring = originalFilename.substring(originalFilename.lastIndexOf(".") + 1);

        StorePath storePath = storageClient.uploadFile(file.getInputStream(), file.getSize(), substring, null);
        return storePath.getFullPath();
    }

    public static String createUploadFile(FastFileStorageClient storageClient,File file) throws FileNotFoundException {
        UploadUtils uploadUtils = new UploadUtils();
        return uploadUtils.uploadFile(storageClient,file);
    }

    public static String createUploadFile(FastFileStorageClient storageClient,MultipartFile file) throws IOException {
        UploadUtils uploadUtils = new UploadUtils();
        return uploadUtils.uploadFile(storageClient,file);
    }
}
